import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Home.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  }
  ,
  {
    path: '/goods',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/Goods.vue')
  }
  ,
  {
    path: '/order',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue')
  }
  ,
  {
    path: '/free',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/Free.vue')
  }
  ,
  {
    path: '/user',
    name: 'about',
    redirect:'/user/cart',
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    children:[
      {
        path:'cart',
        component:()=>import('../components/user/Cart.vue')
      }
    ]
    //路由独享守卫
    // beforeEnter:(to,from,next)=>{
    //   let token=localStorage.getItem('x-auth-token')
    //   if(token){
    //     next()
    //   }else{
    //     alert('请登录')
    //   }
    // }
  }
  ,
  {
    path: '/details',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/Details.vue')
  },{
    path:'*',
    name:'error',
    component:()=>import ('../components/Error.vue')
  }
]

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
})
/* 对多次点击路由报错 */
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
  return originalPush.call(this, location).catch(err => {})
};
// router.beforeEach((to,from,next)=>{
//   let token=localStorage.getItem('x-auth-token')
//   if(to.path=='/user'){
//       if(token){
//     next()
//   }else{
//     store.dispatch('showToast/asyncChangeToast',{
//       isshow:true,
//       msg:'请登录',
//       type:'danger'
//     })
//   }
//   return 
//   }

//   next();
// }

  
  // )
export default router
